<?php
namespace app\api\controller;

use app\api\common\controller\Api;
use think\facade\Db;
// use app\api\common\controller\MidOrder;
use app\api\model\Order as modelOrder;
use app\api\common\controller\PriceSystem;


/**
 * @Apidoc\Title("Demo.php")
 * @Apidoc\Group("api")
 */
class Cashier extends Api
{
	public function __construct(){ 
   
    }
    //收银端订单列表
     public function getOrderList(){
         $state=input('status');
         $page=input('page')?input('page'):1;
         $size=input('size')?input('size'):20;
         $keywords=input('keywords');
         $phone=input('phone');
         $refund=input('refund');
         $source=Db::name('system_admin')->where('username',$phone)->value('source');
         $where=[];
         if(strlen($state)>0) array_push($where,['orders_status','=',$state]);
         if($keywords){
            
            $member_id=Db::name('members')->where('members_mobile','like','%'.$keywords.'%')->column('members_id');

            if($member_id){
                array_push($where,['members_id','in',$member_id]);
            }else{
                array_push($where,['out_trade_no','like','%'.$keywords.'%']);
            }
         }
         if($source){
            array_push($where,['source','=',$source]);
         }
         if($refund){
            array_push($where,['orders_status','=',2]);
            array_push($where,['amount','>',0]);
            array_push($where,['refund_time','=',0]);
            // $refund_orders_id=Db::name('refund')->whereTime('created_at', 'today')->column('orders_id');
            // dd($refund_orders_id);
         }
         $orderData=Db::name('order')->field('id,out_trade_no,tel,name,orders_status,users,members_id,refund_time')->where($where)->order('create_time desc')->page($page,$size)->select()->toArray();
         foreach ($orderData as $k => $v) {
            $order_server=Db::name('order_server')->where('order_id',$v['id'])->select()->toArray();
            $users=Db::name('user')->whereIn('id',$v['users'])->find();
            if($users){
                     $orderData[$k]['name']=$users['name'];
                     $orderData[$k]['tel']=$users['phone'];
            }else{
                    $users=Db::name('members')->whereIn('members_id',$v['members_id'])->find();
                     $orderData[$k]['name']=base64_decode($users['members_nickname']);
                     $orderData[$k]['tel']=$users['members_mobile'];
            }
           
            foreach ($order_server as $kk => $vv) {
                if($vv['type']==1){
                        $ticket=Db::name('ticket')->where('id',$vv['server_id'])->field('title,thumb')->find();
                        $order_server[$kk]['title']=$ticket['title'];
                        $order_server[$kk]['image']=$ticket['thumb'];
                }elseif($vv['type']==2){
                    $lease=Db::name('lease')->where('id',$vv['server_id'])->find();
                     $order_server[$kk]['title']=$lease['title'];
                     $order_server[$kk]['image']=$lease['thumb'];
                     if($vv['time_id']) $order_server[$kk]['time_interval']=Db::name('spec_item')->where('id',$vv['time_id'])->value('item');
                }elseif ($vv['type']==3) {
                      $goods=Db::name('goods')->where('products_id',$vv['server_id'])->find();
                      $title=$goods['products_title'];
                      $image=$goods['cover_picture'];
                      $spec_item=Db::name('goods_value')->where('products_id',$vv['server_id'])->where('sku_val',$vv['spec'])->value('sku');
                      $order_server[$kk]['title']=$title;
                      $order_server[$kk]['image']=$image;
                      if($spec_item){
                         $order_server[$kk]['spec_item']=$spec_item;
                      }
                }elseif($vv['type']==4){
                     $package=Db::name('package')->where('id',$vv['server_id'])->find();
                     $title=$package['title'];
                      $image=$package['thumb'];
                }
            }
            $orderData[$k]['childern']=$order_server;
         }
         $this->result('success',$orderData,200,'json');  
     }
      public function getOrderDetail(){
        $order_id=input('order_id');
        $where=[
            ['id','=',$order_id]
        ];
        $orderData=Db::name('order')->field('id,out_trade_no,tel,name,use_time,create_time,code,orders_status,users,refund_time')->where($where)->find();
        $refund=Db::name('refund')->where('orders_id',$order_id)->value('amount');
        $orderData['refund_amount']=$refund?$refund:0;
        $orderData['users']=Db::name('user')->whereIn('id',$orderData['users'])->select();
        $order_server=Db::name('order_server')->where('order_id',$orderData['id'])->select()->toArray();
        foreach ($order_server as $kk => $vv) {
                if($vv['type']==1){
                        $ticket=Db::name('ticket')->where('id',$vv['server_id'])->field('title,thumb')->find();
                        $order_server[$kk]['title']=$ticket['title'];
                        $order_server[$kk]['image']=$ticket['thumb'];
                }elseif($vv['type']==2){
                    $lease=Db::name('lease')->where('id',$vv['server_id'])->find();
                     $order_server[$kk]['title']=$lease['title'];
                     $order_server[$kk]['image']=$lease['thumb'];
                     if($vv['time_id']) $order_server[$kk]['time_interval']=Db::name('spec_item')->where('id',$vv['time_id'])->value('item');
                }elseif ($vv['type']==3) {
                    $goods=Db::name('goods')->where('products_id',$vv['server_id'])->find();
                      $title=$goods['products_title'];
                      $image=$goods['cover_picture'];
                      $spec_item=Db::name('goods_value')->where('products_id',$vv['server_id'])->where('sku_val',$vv['spec'])->value('sku');
                      $order_server[$kk]['title']=$title;
                      $order_server[$kk]['image']=$image;
                      if($spec_item){
                         $order_server[$kk]['spec_item']=$spec_item;
                      }
                }elseif($vv['type']==4){
                        $package=Db::name('package')->where('id',$vv['server_id'])->find();
                        
                        $order_server[$kk]['title']=$package['title'];
                        $order_server[$kk]['image']=$package['thumb'];
                        $package_attr=Db::name('package_attr')->where('package_id',$vv['server_id'])->select()->toArray();
                        foreach ($package_attr as $k1 => $v1) {
                            if($v1['server_type']==2){
                                $package_attr[$k1]['server_name']=Db::name('lease')->where('id',$v1['server_id'])->value('title');
                            }elseif($v1['server_type']==3){
                                 $package_attr[$k1]['server_name']=Db::name('goods')->where('products_id',$v1['server_id'])->value('products_title');
                            }
                        }
                        $order_server[$kk]['attr']=$package_attr;
                }
        }
        $orderData['childern']=$order_server;
        $orderData['create_time']=date('Y-m-d H:i:s',$orderData['create_time']);
        $orderData['use_time']=date('Y-m-d',$orderData['use_time']);
        $this->result('success',$orderData,200,'json'); 
     }
  
}
